package com.gxuwz.attend.dao.instructor;

import com.gxuwz.attend.entity.Leave;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class AuditDao {
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    // 更新请假申请状态
    public int updateLeaveStatus(String leaveID, String status, String opinion) {
        String sql = "UPDATE sys_leave SET status = ?, opinion = ?, audittime = NOW() WHERE leaveID = ?";
        return  jdbcTemplate.update(sql, status, opinion, leaveID);
    }

    public List<Leave> getLeaveRequestsByInstructor(String instID) {
        String sql = "SELECT * " +
                "FROM sys_leave " +
                "WHERE stuNo IN ( " +
                "    SELECT stuID " +
                "    FROM sys_student " +
                "    WHERE classID IN ( " +
                "        SELECT classID " +
                "        FROM sys_classes " +
                "        WHERE instID = ? " +  // 使用 instID 来查询辅导员
                "    ) " +
                ") " +
                "AND status = '0';";  // 只获取待审核的记录

        return jdbcTemplate.query(sql, new Object[]{instID}, (rs, rowNum) -> {
            Leave leave = new Leave();
            leave.setLeaveID(rs.getString("leaveID"));
            leave.setCourseID(rs.getString("courseID"));
            leave.setReason(rs.getString("reason"));
            leave.setDaynum(rs.getInt("daynum"));
            leave.setStuNo(rs.getString("stuNo"));

            // 获取日期时间，使用 getTimestamp 而非 getTime
            leave.setApplytime(String.valueOf(rs.getTimestamp("applytime")));
            leave.setStatus(rs.getString("status"));
            leave.setAudittime(String.valueOf(rs.getTimestamp("audittime")));
            leave.setOpinion(rs.getString("opinion"));

            return leave;
        });
    }


}
